package org.xclcharts.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.HorizontalScrollView;

import org.xclcharts.chart.BarChart;
import org.xclcharts.chart.BarData;
import org.xclcharts.common.DensityUtil;
import org.xclcharts.common.IFormatterDoubleCallBack;
import org.xclcharts.common.IFormatterTextCallBack;
import org.xclcharts.renderer.XEnum;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 杰 on 2017/12/13.
 */

public class BarChartView extends BaseChart {

    private String TAG = "BarChart01View";
    private BarChart chart = new BarChart();
    private int maxSize = 10;
    private int padding;
    private int UIWidth;

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public void setPadding(int padding) {
        this.padding = padding;
    }

    //标签轴
    private List<String> chartLabels = new ArrayList<String>();
    private List<BarData> chartData = new ArrayList<BarData>();

    public BarChartView(Context context) {
        super(context);
        padding = DensityUtil.dip2px(context, 20);
        UIWidth = DensityUtil.getScreenWidth(context);
        initView();
    }

    public BarChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        padding = DensityUtil.dip2px(context, 20);
        UIWidth = DensityUtil.getScreenWidth(context);
        initView();
    }

    public void setChartLabels(List<String> chartLabels) {
        this.chartLabels = chartLabels;
    }

    public void setChartData(List<BarData> chartData) {
        this.chartData = chartData;
        chartRender();
    }

    private void initView() {
        chartRender();
        //綁定手势滑动事件
        this.bindTouch(this, chart);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //图所占范围大小
        chart.setChartRange(w, h);
    }

    public void setAxisMax(int max) {
        chart.getDataAxis().setAxisMax(max);
    }

    public void setAxisMin(int min) {
        chart.getDataAxis().setAxisMin(min);
    }


    private void chartRender() {
        try {
            //设置绘图区默认缩进px值,留置空间显示Axis,Axistitle....
            int[] ltrb = getBarLnDefaultSpadding();
            chart.setPadding(30, 0,
                    0, 50);

            //标题
            //数据源
            chart.setCategories(chartLabels);
            chart.setDataSource(chartData);
            chart.enablePanMode();
            chart.disableScale();

            //数据轴
            if (chartData != null && !chartData.isEmpty()) {
                BarData barData = chartData.get(0);
                List<Double> dataSet = barData.getDataSet();
                double max = 0d;
                for (double v : dataSet) {
                    if (v > max) {
                        max = v;
                    }
                }
                if (max == 0) {
                    chart.getDataAxis().setAxisSteps(0.1);
                    chart.getDataAxis().setAxisMax(1);
                    chart.getDataAxis().setAxisMin(0);
                } else {
                    chart.getDataAxis().setAxisSteps((max * 1.1) / 10);
                    chart.getDataAxis().setAxisMax(max * 1.1);
                    chart.getDataAxis().setAxisMin(0);
                }
            }
            chart.getDataAxis().getAxisPaint().setColor(Color.TRANSPARENT);
            chart.getDataAxis().hideFirstTick();
            chart.getDataAxis().hideTickMarks();
            chart.getCategoryAxis().getTickLabelPaint().setColor(0xff55DFFF);
            chart.getCategoryAxis().setTickLabelMargin(20);
            chart.getCategoryAxis().getTickLabelPaint().setTextSize(26);
            chart.getCategoryAxis().getAxisPaint().setColor(0xff55DFFF);
            chart.getDataAxis().hide();

            //指隔多少个轴刻度(即细刻度)后为主刻度
            chart.getDataAxis().setDetailModeSteps(10);

            //背景网格
            chart.getPlotGrid().hideHorizontalLines();
            chart.getPlotGrid().hideVerticalLines();
            chart.getPlotGrid().hideEvenRowBgColor();
            chart.getPlotGrid().hideOddRowBgColor();

            //定义数据轴标签显示格式
            chart.getDataAxis().setLabelFormatter(new IFormatterTextCallBack() {

                @Override
                public String textFormatter(String value) {
                    // TODO Auto-generated method stub
                    Double tmp = Double.parseDouble(value);
                    DecimalFormat df = new DecimalFormat("#0");
                    String label = df.format(tmp).toString();
                    return (label);
                }
            });

            //在柱形顶部显示值
            chart.getBar().setItemLabelVisible(false);
            chart.getBar().setItemLabelStyle(XEnum.ItemLabelStyle.OUTER);
            chart.getBar().getItemLabelPaint().setColor(0xffffffff);
            chart.getBar().getItemLabelPaint().setTextSize(20);

            chart.getBar().setBarRoundRadius(15);
            chart.getBar().setBarStyle(XEnum.BarStyle.ROUNDBAR);
//            chart.getCategoryAxis().hideAxisLine();
            chart.getCategoryAxis().hideTickMarks();
//            chart.getCategoryAxis().hideAxisLabels();
            chart.getBar().setBarTickSpacePercent(0.9f);
            chart.getBar().setBarMaxPxWidth(20);
            chart.getBar().setBarInnerMargin(0);
            chart.setMaxSize(10);
//            chart.getDataAxis().hide();

            chart.disablePanMode();
            chart.disableHighPrecision();
            //设定格式
            chart.setItemLabelFormatter(new IFormatterDoubleCallBack() {
                @Override
                public String doubleFormatter(Double value) {
                    // TODO Auto-generated method stub
                    DecimalFormat df = new DecimalFormat("#0.#");
                    String label = df.format(value).toString();
                    return label;
                }
            });

            //隐藏Key
            chart.getPlotLegend().hide();

            chart.getClipExt().setExtTop(10.f);


            //柱形和标签居中方式
            chart.setBarCenterStyle(XEnum.BarCenterStyle.TICKMARKS);
            invalidate();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getParent() instanceof HorizontalScrollView) {
            HorizontalScrollView parent = (HorizontalScrollView) getParent();
            int width = UIWidth - (2 * padding);
            setMeasuredDimension((width / maxSize) * chartLabels.size(), measureHeight(heightMeasureSpec));
        } else {
            setMeasuredDimension(measureWidth(widthMeasureSpec),
                    measureHeight(heightMeasureSpec));
        }
    }

    @Override
    public void render(Canvas canvas) {
        try {
            chart.render(canvas);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

}
